package com.zj.ssm.config;
 
 
 
import com.zj.ssm.bean.AliPayBean;
import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
 
import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
 
@Data
@Configuration
public class Alipay {
 
    /**日志对象*/
    private static final Logger logger = LoggerFactory.getLogger(Alipay.class);
 
    private final String format = "json";
 
    /**
     * appId
     */
    @Value("2021000119620372")
    private String appId;
 
    /**
     * 商户私钥
     */
    @Value("MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQDDFMv12sNER/2Q/bhDPhBpUAX3aKpSJ0Av8UytLZqmNoGnLPmcT8hsCb5JHcXfSaW6NMtfizv286JKUOBVPMiYKJf+AIGXT+gdXDsEVgkQF7QQ4aCbU0vsd8ChbcjYn8Mrnhsq/a3rxajx6W4PDQ4k5XtW6FbUIDedRExZJoC6qWwdKubr5UByuKXbZQ/BtJLCEK1dQXK9X+oNfW/XuHQL2VdQqwD/RH/b4CzQuFzupOHwgwxCKa4YbT7nQiROenjy3gCXxq/OOgDaoAtaj67TraQtdwZ4pvuq5+GOiU6jRTZqPkDdMXH+Tdfc00LsyW2sn0d35o++E9lHQESP8YQdAgMBAAECggEBAKnLZiMdP6wkxMz4KPNOB9bB8g9/qA9NXo7cBZMOCHb3Ji4t9SxaPOXcLSVSprFMVtUVspX8iDrPn8fzUO6fszDM1E4efq0Gvc8gQbXqdITvPKkc2T+YZpLxQFbBSXlSxPsn6BmpJ0H5jqzQMh0jjdYLbykbnMeRKxbVJWbLHmxYGSxgfo559WoGarVR8Yoh0dyRgCPuFPLqSW4v9+zhlH5nzA2gBHPXA7xlzMRybbyjPsxlrpZQ+2bhIShQr8jBvEGSx9nFflsLaxfbKTW3pBC23m2o5mkKC+BwtHBWUxFiUoh2vcuovp74NUL7TRZH05D0EQKhrGSI1FMhJJEUmAECgYEA5q/R0XBBnp0MejlfEfw+9Q5CZvazcO+bS2HN4SNmXrTsodAhcsCatU8wZENJLXk3jjivWYozXR9dOZhROcfLnetwH1VJqnImNdA69maKZ7czx0vTI4tY01Nw6AtYIihIlKlmTIODHv2l0cHiGfSW8dPLfNdy8jMjd8Ed6S0lbQECgYEA2HzJtWa0iL9L6QPwHQHFYBlzeOIfx54uerYXapmHrARfXUWsZkandZrVrJd0eKkvs5BQKWWuvDeF6MbSuI9h/at+dfyoxMRgq6xc34ZprG3PmE24p/3q477/nCZqEwRrMAs2MNGa0Yb+emZfWyxcVrzC8asl037etm+EeShlKx0CgYEAnSpoPtLGQjQpThRMS0qZ8gc3AkNN2cHFAcSuqZaepiOJnGzafP3v6Vr8UzLsZkjRqtm/zbc0a1t0/kph/IW8mejPyOkoeTGwCrXFlh4iaasNYditDuhfKgsNP2qLLypW2MHY6oQnDzQshw4a5ygWtsdfObznmU9x+KLfp/+UIgECgYAl5Fmt7acl1E3mIDFT8m5orqu+ed3UI8TA58RbfTn5Ct7iRYKyeCVxeUNzLSNOx1Y5GMZfKJQYD6PZpYtWkIo3S2AcKLglkLXW9ple6njunF3cq6XpiZEmgdTdsP+aCoj0ZNH8Y4O0rus141tHaxFSgUGh6TH6vdcfmY9XYZGrpQKBgQCaUwlVYktDWrzmt1p3fk/9vlfcMzI/eYbsPpDKJQUincASNegzCiJHSmjIg8gmhxvVrP08Zphiya8XwgKNvPWk4cHKqn7eicvIxwtXTJ0xaC7EmoKSimkobubp1cf3tr7s5zmIaxRYp2bzX8QNuiPG8ucU0kBwfR3ODPTGt+XgOQ==")
    private String privateKey;
 
    /**
     * 支付宝公钥
     */
    @Value("MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAoU+dN/I8sgDwgmIc6fvRzTZtjzgcGAym2FW6yptMjFonsYZhwX5bnVb/4AnGNuSbsAdctivV0eHnKmfquQJ+mRnYSVI5zWPkPiI+mjIuG96TNsdRRN/eiRHwX4Tk60iESW69/jJsmFTfwfOs1LBZHrkxo2AkWpibPr2QXnEzzzD8qh91L9WXOWM+aH/dV404Fl6ivbD1sxf3Tt3PwzvY2YLJY2JRSE7wvgVWpLJ0knCC82vgDdB7szkyjoI6woFuZbPFL4IFQX8AhzjSq/GWxkLd5JyiL8nbNLfnEIiYwQsYmHoj5ffg0uYHk4BRuJZrLPV31n6yskHb6fYSJrcpnwIDAQAB")
    private String publicKey;
 
    /**
     * 服务器异步通知页面路径，需要公网能访问到
     */
    @Value("http://localhost:8080/ssm/error_url.html")
    private String notifyUrl;
 
    /**
     * 服务器同步通知页面路径，填写自己的成功页面路径
     */
    @Value("http://localhost:8080/ssm/return_url.html")
    private String returnUrl;
 
    /**
     * 签名方式
     */
    @Value("RSA2")
    private String signType;
 
    /**
     * 字符编码格式
     */
    @Value("utf-8")
    private String charset;
 
    /**
     * 支付宝网关
     */
    @Value("https://openapi.alipaydev.com/gateway.do")
    private String gatewayUrl;
 
    public String pay(AliPayBean aliPayBean) throws AlipayApiException {
 
        AlipayClient alipayClient = new DefaultAlipayClient(
                gatewayUrl, appId, privateKey, format, charset, publicKey, signType);
 
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(returnUrl);
        alipayRequest.setNotifyUrl(notifyUrl);
        alipayRequest.setBizContent(JSON.toJSONString(aliPayBean));
        logger.info("封装请求支付宝付款参数为:{}", JSON.toJSONString(alipayRequest));
 
        String result = alipayClient.pageExecute(alipayRequest).getBody();
        logger.info("请求支付宝付款返回参数为:{}", result);
 
        return result;
    }
}
 
 